package com.yzw.course.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzw.common.domain.EduSubject;
import com.yzw.common.exception.CustomException;
import com.yzw.common.mapper.EduSubjectMapper;
import com.yzw.common.vo.SubjectVo;
import com.yzw.course.excel.SubjectData;
import com.yzw.course.excel.SubjectExcelListener;
import com.yzw.course.service.EduSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject>
        implements EduSubjectService {

    @Override
    public void importExcel(MultipartFile excel) {
        //1 获取文件输入流
        InputStream inputStream = null;
        try {
            inputStream = excel.getInputStream();
            // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(this)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void saveFormExcel(List<SubjectData> cachedDataList) {
        if (ObjectUtils.isEmpty(cachedDataList)) {
            throw new CustomException(20001, "文件数据为空");
        }

        Map<String, List<String>> map = new HashMap<>();

        // cachedDataList封装成map
        for (SubjectData subjectData : cachedDataList) {
            List<String> list = map.get(subjectData.getOneName());
            if (list == null) {
                list = new ArrayList<>();
            }
            list.add(subjectData.getTwoName());
            map.put(subjectData.getOneName(), list);
        }

        // 减少sql语句
        List<EduSubject> subjects = this.list();
        Map<String, EduSubject> existMap = new HashMap<>();
        for (EduSubject e : subjects) {
            existMap.put(e.getTitle(), e);
        }

        map.forEach((one, twoList) -> {
            EduSubject existOneSubject = existMap.get(one);
            if (existOneSubject == null) {
                existOneSubject = new EduSubject();
                existOneSubject.setTitle(one);
                existOneSubject.setParentId("0");
                this.save(existOneSubject);
            }
            List<EduSubject> twoSubjects = new ArrayList<>();
            for (String title : twoList) {
                EduSubject existTwoSubject = existMap.get(title);
                if (existTwoSubject == null) {
                    existTwoSubject = new EduSubject();
                    existTwoSubject.setTitle(title);
                    existTwoSubject.setParentId(existOneSubject.getId());
                    existMap.put(title,existTwoSubject);
                    twoSubjects.add(existTwoSubject);
                }
            }
            this.saveBatch(twoSubjects);
        });
    }

    @Override
    public List<SubjectVo> getAllSubject() {
        List<SubjectVo> subjectVos = new ArrayList<>();
        List<EduSubject> eduSubjects = this.list();
        for (EduSubject eduSubject : eduSubjects) {
            SubjectVo subjectVo = new SubjectVo();
            BeanUtils.copyProperties(eduSubject,subjectVo);
            subjectVos.add(subjectVo);
        }

        // 递归查询
        List<SubjectVo> subjectVoList = new ArrayList<>();
        for (SubjectVo treeNode : subjectVos) {
            if("0".equals(treeNode.getParentId())){
                subjectVoList.add(findChildren(treeNode,subjectVos));
            }
        }
        return subjectVoList;
    }

    public static SubjectVo findChildren(SubjectVo subjectVo, List<SubjectVo> subjectVos) {
        subjectVo.setChildren(new ArrayList<>());
        for (SubjectVo vo : subjectVos) {
            if (subjectVo.getId().equals(vo.getParentId())){
                subjectVo.getChildren().add(findChildren(vo,subjectVos));
            }
        }
        return subjectVo;
    }
}
